<?php
class Default_Form_ValidateDeveloper{
	
	//CHUA NHUNG THONG BAO LOI CUA FORM
	protected $_messagesError = null;
	
	//MANG CHUA DU LIEU SAU KHI KIEM TRA
	protected $_arrData;
	
	public function __construct($arrParam = array(),$options = null){
		//========================================
		// KIEM TRA user_name
		//========================================
		
		if($arrParam['action'] == 'add'){
			$options = array('table'=>'developer_files','field'=>'name_app');
		}else if($arrParam['action'] == 'edit'){
			$clause = ' id !=' . $arrParam['id'];
			$options = array('table'=>'developer_files','field'=>'name_app','exclude'=>$clause);
		}
		
		$validator = new Zend_Validate();
		
		
		$validator->addValidator(new Zend_Validate_NotEmpty(),true)
				  ->addValidator(new Zend_Validate_StringLength(3,32),true)
				  ->addValidator(new Zend_Validate_Regex('#^[a-zA-Z0-9\-_\.\s]+$#'),true)
				  ->addValidator(new Zend_Validate_Db_NoRecordExists($options),true);
		if(!$validator->isValid($arrParam['name_app'])){
			$message = $validator->getMessages();
			$this->_messagesError['name_app'] = 'Name Application: ' . current($message);
			$arrParam['name_app'] = '';
		}		

		//========================================
		// KIEM TRA Link_app
		//========================================
		$upload = new Zend_File_Transfer_Adapter_Http();
		$fileInfo = $upload->getFileInfo('link_app');
		$fileName = $fileInfo['link_app']['name'];
		
		if(!empty($fileName)){
		
			//$upload->addValidator('Extension',true,array('jpg','gif','png'),'link_app');
			$upload->addValidator('Size',true,array('min'=>'2KB','max'=>'1000000KB'),'link_app');
			if(!$upload->isValid('link_app')){
				$message = $upload->getMessages();
				$this->_messagesError['link_app'] = 'Link App: ' . current($message);
				
			}
		}
		
		//========================================
		// KIEM TRA Link_img
		//========================================
		$upload = new Zend_File_Transfer_Adapter_Http();
		$fileInfo = $upload->getFileInfo('link_img');
		$fileName = $fileInfo['link_img']['name'];
		
		if(!empty($fileName)){
		
			$upload->addValidator('Extension',true,array('jpg','gif','png'),'link_img');
			$upload->addValidator('Size',true,array('min'=>'2KB','max'=>'1000KB'),'link_img');
			if(!$upload->isValid('link_img')){
				$message = $upload->getMessages();
				$this->_messagesError['link_img'] = 'Link Img: ' . current($message);
			}
		}
		

		//========================================
		// KIEM TRA group_app
		//========================================
		if($arrParam['group_app'] == 0){
			$this->_messagesError['group_app'] = 'Group: Please select a group app';
		}
		
		
		//========================================
		// KIEM TRA type_app
		//========================================
		if($arrParam['type_app'] == 0){
			$this->_messagesError['group_app'] = 'Group: Please select a type app';
		}
		
		
		//========================================
		// KIEM TRA status
		//========================================
		if(empty($arrParam['status']) || !isset($arrParam['status'])){
			$arrParam['status'] = 0;
		}
		
		//========================================
		// KIEM TRA sign
		//========================================
		$validator = new Zend_Validate();
		$validator->addValidator(new Zend_Validate_NotEmpty(),true)
				  ->addValidator(new Zend_Validate_StringLength(10),true);
		if(!$validator->isValid($arrParam['des_app'])){
			$message = $validator->getMessages();
			$this->_messagesError['des_app'] = 'Description: ' . current($message);
			$arrParam['des_app'] = '';
		}	
		
		//========================================
		// TRUYEN CAC GIA TRI DUNG VAO MANG $_arrData
		//========================================
		$this->_arrData = $arrParam;
	}
	
	//Kiem tra Error 
	//return true neu co loi xuat hien
	public function isError(){		
		if(count($this->_messagesError)>0){
			return true;
		}else{
			return false;
		}
	}
	
	//Tra ve mot mang cac loi
	public function getMessageError(){
		return $this->_messagesError;
	}
	
	//Tra ve mot mang du lieu sau khi kiem tra
	public function getData($options = null){
		if($options['upload'] == true){
			$this->_arrData['link_app'] = $this->uploadApp();
			$this->_arrData['link_img'] = $this->uploadImg();
		}
		return $this->_arrData;
	}
	
	/*========================================
	* 1. Upload user_avatar
	* 2. Resize kich thuoc (100x100 va 450x450)
	* 3. Tra ve ten tap tin upload
	*========================================*/
	public function uploadApp(){
		//Duong dan den thu muc upload
		$upload_dir = FILES_PATH . '/developer';
		
		//========================================
		// UPLOAD FILE application
		//========================================
		
		$upload = new Zendvn_File_Upload();
		$fileInfo = $upload->getFileInfo('link_app');
		$fileName = $fileInfo['link_app']['name'];
		
		if(!empty($fileName)){
			$fileName = $upload->upload('link_app', $upload_dir . '/app',
							 array('task'=>'rename'),'app_');
// 			if($this->_arrData['action'] == 'edit'){
// 				$upload->removeFile($upload_dir . '/app/' . $this->_arrData['current_user_avatar']);
// 			}
		}else{
// 			if($this->_arrData['action'] == 'edit'){
// 				$fileName = $this->_arrData['current_user_avatar'];
// 			}
		}
		
		return $fileName;
	}
	
	public function uploadImg(){
		//Duong dan den thu muc upload
		$upload_dir = FILES_PATH . '/developer';
	
		//========================================
		// UPLOAD FILE application
		//========================================
	
		$upload = new Zendvn_File_Upload();
		$fileInfo = $upload->getFileInfo('link_img');
		$fileName = $fileInfo['link_img']['name'];
	
		if(!empty($fileName)){
			$fileName = $upload->upload('link_img', $upload_dir . '/imgs/orignal/',array('task'=>'rename'),'app_');
				$thumb = Zendvn_File_Images::create($upload_dir . '/imgs/orignal/' . $fileName);
				$thumb->resize(100,100)->save($upload_dir . '/imgs/img110x110/' . $fileName);

				$thumb->resize(20,20)->save($upload_dir . '/imgs/img20x20/' . $fileName);
		
				if($this->_arrData['action'] == 'edit'){
					$upload->removeFile($upload_dir . '/app/' . $this->_arrData['current_user_avatar']);
				}
		}else{
			// 			if($this->_arrData['action'] == 'edit'){
			// 				$fileName = $this->_arrData['current_user_avatar'];
			// 			}
		}
	
		return $fileName;
	}
	
	
	
}